package com.achievement.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import com.achievement.domain.dto.PaperReshipDTO;
import com.achievement.domain.entity.PaperReshipEntity;
import com.achievement.domain.vo.PaperReshipVO;
import com.achievement.mapper.PaperReshipMapper;
import com.achievement.mappings.paper.PaperMapping;
import com.achievement.service.IPaperReshipService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
* @author CNKI
* @description 针对表【biz_paper_reship(科研成果-学术论文-论文-转载情况)】的数据库操作Service实现
* @createDate 2025-02-17 16:48:50
*/
@Service
public class PaperReshipServiceImpl extends ServiceImpl<PaperReshipMapper, PaperReshipEntity>
    implements IPaperReshipService {

    @Autowired
    private PaperMapping mapping;

    /**
     * 新增论文转载情况
     *
     * @param dto dto
     * @return boolean
     */
    @Override
    public boolean addPaperReship(PaperReshipDTO dto) {

        if (ObjectUtil.isNull(dto)){
            return true;
        }

        String UUID = IdUtil.randomUUID().replace("-", "");

        dto.setId(UUID);

        PaperReshipEntity paperReshipEntity = mapping.PaperReshipDto2Entity(dto);

        return this.save(paperReshipEntity);
    }

    /**
     * 编辑论文转载情况
     *
     * @param dto dto
     * @return boolean
     */
    @Override
    public boolean editPaperReship(PaperReshipDTO dto) {

        PaperReshipEntity paperReshipEntity = mapping.PaperReshipDto2Entity(dto);

        return  this.updateById(paperReshipEntity);
    }

    /**
     * 删除论文转载情况
     *
     * @param id id
     * @return boolean
     */
    @Override
    public boolean delPaperReship(String id) {
        return this.removeById(id);
    }

    /**
     * 根据论文id，查询转载情况列表
     * @param id id
     * @return List<PaperReshipVO>
     */
    @Override
    public List<PaperReshipVO> getInfoById(String id) {

        LambdaQueryWrapper<PaperReshipEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PaperReshipEntity::getPaperId,id);
        List<PaperReshipEntity> list = this.list(queryWrapper);

        List<PaperReshipVO> voList = list.stream().map(mapping::PaperReshipEntity2Vo).collect(Collectors.toList());
        return voList;
    }
}




